package com.google.code.jbauble.mvc.support;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;

import com.google.code.jbauble.mvc.DefaultConfiguration;
import com.google.code.jbauble.mvc.WebContext;

public class UrlTool {
	private static String siteUrl = null;
	
	static {
		siteUrl = WebContext.getInstance().getConfig().getSiteUrl();
		if (siteUrl.endsWith("/")) {
			siteUrl = siteUrl.substring(0, siteUrl.length() - 1);
		}
	}
	
	public String get(String url) {
		if (url.startsWith("/"))
			return siteUrl + url;
		return siteUrl + "/" + url;
	}
	
	public String get(String controller, String action) {
		return get(controller, action, null);
	}
	
	public String get(String controller, String action, String params) {
		String url = "/" + controller + "/" + action;
		if (params == null || params.length() < 1)
			return get(url);
		
		url += "?" + params;
		return get(url);
	}
	
	/**
	 * 返回需要的String
	 * @return
	 */
	public String encode(String s) {
		if(s == null || s.isEmpty()) return "";
		try {
            return URLEncoder.encode(s,"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return URLEncoder.encode(s);
        }
	}	
}
